/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Release.java
 *
 * Created on 27 avr. 2011, 23:25:38
 */
/**
 *
 * @author Arthur
 */

package ihm_clarifiee.Presentation;

import ihm_clarifiee.Control.CProjet;
import ihm_clarifiee.Presentation.model_table.model_table_listeRelease;
import ihm_clarifiee.Control.CRelease;
import ihm_clarifiee.Control.CSprint;
import ihm_clarifiee.Presentation.model_table.model_table_allsprints;
import ihm_clarifiee.Presentation.popup.DetailSprint;
import ihm_clarifiee.Presentation.popup.base_affichage;
import ihm_clarifiee.Presentation.popup.popup;
import ihm_clarifiee.Presentation.popup.popup_release;
import ihm_clarifiee.Presentation.popup.popup_sprint;
import ihm_clarifiee.application.Histoire;
import ihm_clarifiee.application.Release;
import ihm_clarifiee.application.Sprint;
import ihm_clarifiee.application.Tache;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import javax.swing.JOptionPane;

import javax.swing.JPanel;

import org.jfree.ui.RefineryUtilities;

import sun.awt.VerticalBagLayout;

public class PRelease extends javax.swing.JPanel implements popup {

  //  private CBacklog controle;
    private model_table_allsprints md;
     private model_table_listeRelease mda;
    private CRelease  controle;
    private PListeReleases ptest;





     public PRelease(CRelease p) {
    	 controle =p;
    	 System.out.println("PRelease.java");
         initComponents();
         System.out.println("PRelease.java2");
         md=new model_table_allsprints();
         mda=new model_table_listeRelease();
         tbl_principal.setModel(md);
         tbl_active.setModel(mda);
         initModel();
         updateliste();
     }
     
   public void setListe(PListeReleases pdt){
	   ptest=pdt;
   }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initModel ()
    {
        tbl_principal.getColumnModel().getColumn(0).setMinWidth(100);
        tbl_principal.getColumnModel().getColumn(0).setPreferredWidth(100);
        tbl_principal.getColumnModel().getColumn(0).setMaxWidth(100);
        tbl_principal.getColumnModel().getColumn(1).setMinWidth(10);
        tbl_principal.getColumnModel().getColumn(1).setPreferredWidth(50);
        tbl_principal.getColumnModel().getColumn(1).setMaxWidth(50);
        tbl_principal.getColumnModel().getColumn(2).setMinWidth(80);
        tbl_principal.getColumnModel().getColumn(2).setPreferredWidth(150);
        tbl_principal.getColumnModel().getColumn(2).setMaxWidth(150);
        tbl_principal.getColumnModel().getColumn(3).setMinWidth(80);
        tbl_principal.getColumnModel().getColumn(3).setPreferredWidth(150);
        tbl_principal.getColumnModel().getColumn(3).setMaxWidth(150);
        tbl_principal.getColumnModel().getColumn(4).setMinWidth(100);
        tbl_principal.getColumnModel().getColumn(4).setPreferredWidth(300);
        tbl_principal.getColumnModel().getColumn(4).setMaxWidth(4000);
        tbl_principal.getColumnModel().getColumn(0).setResizable(true);
        tbl_principal.getColumnModel().getColumn(1).setResizable(true);
        tbl_principal.getColumnModel().getColumn(2).setResizable(true);
        tbl_principal.getColumnModel().getColumn(3).setResizable(true);
         tbl_principal.getColumnModel().getColumn(4).setResizable(true);
        tbl_principal.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);

       tbl_active.getColumnModel().getColumn(0).setMinWidth(20);
       tbl_active.getColumnModel().getColumn(0).setPreferredWidth(100);
       tbl_active.getColumnModel().getColumn(0).setMaxWidth(2000);
       tbl_active.getColumnModel().getColumn(1).setMinWidth(30);
       tbl_active.getColumnModel().getColumn(1).setPreferredWidth(300);
       tbl_active.getColumnModel().getColumn(1).setMaxWidth(4500);
       tbl_active.getColumnModel().getColumn(2).setMinWidth(100);
       tbl_active.getColumnModel().getColumn(2).setPreferredWidth(150);
       tbl_active.getColumnModel().getColumn(2).setMaxWidth(4000);
       tbl_active.getColumnModel().getColumn(3).setMinWidth(100);
       tbl_active.getColumnModel().getColumn(3).setPreferredWidth(150);
       tbl_active.getColumnModel().getColumn(3).setMaxWidth(4000);
       tbl_active.getColumnModel().getColumn(0).setResizable(true);
       tbl_active.getColumnModel().getColumn(1).setResizable(true);
       tbl_active.getColumnModel().getColumn(2).setResizable(true);
       tbl_active.getColumnModel().getColumn(3).setResizable(true);
       tbl_active.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);



     }


    private void initComponents() {

        panelVertical = new JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbl_principal = new javax.swing.JTable();
        tbl_active = new javax.swing.JTable();
//        bt_ajouter = new javax.swing.JButton();
        bt_graph = new javax.swing.JButton();
        bt_suppr = new javax.swing.JButton();
        bt_modifier = new javax.swing.JButton();
//        bt_monter = new javax.swing.JButton();
//        bt_descendre = new javax.swing.JButton();
        bt_detail = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();


        jScrollPane1.setViewportView(tbl_principal);
        jScrollPane2.setViewportView(tbl_active);


//        bt_ajouter.setText("Ajouter");
//        bt_ajouter.addActionListener(new java.awt.event.ActionListener() {
//            public void actionPerformed(java.awt.event.ActionEvent evt) {
//                bt_ajouterActionPerformed(evt);
//            }
//        });

        bt_suppr.setText("Suppr");

          bt_suppr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_supprActionPerformed(evt);
            }
        });


        bt_modifier.setText("Modifier");
//          bt_monter.setText("Monter2");
//            bt_descendre.setText("Descendre");
        
        bt_graph.setText("Graphe");

        bt_graph.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
              bt_graphActionPerformed(evt);
          }
      });

        bt_detail.setText("Detail");

          bt_detail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_detailActionPerformed(evt);
            }
        });
            bt_modifier.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_modifierActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setText("Liste des Sprints");
        jLabel1.setMaximumSize(new java.awt.Dimension(146, 124));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel2.setText("Release associee");
        jLabel2.setMaximumSize(new java.awt.Dimension(146, 124));



        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(bt_suppr, 98, 98,98)
//                            .addComponent(bt_ajouter, 98, 98, 98)
                            .addComponent(bt_modifier, 98, 98, 98)
                            .addComponent(bt_graph, 98, 98, 98)
                            .addComponent(bt_detail, 98, 98, 98)))
//                             .addComponent(bt_monter, 98, 98, 98)
//                            .addComponent(bt_descendre, 98, 98, 98)))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
//                        .addComponent(bt_ajouter)
//                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_modifier)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_suppr)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_graph)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_detail)
                        .addGap(23, 23, 23)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
//                        .addComponent(bt_monter)
//                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
//                        .addComponent(bt_descendre)
//                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

                         .addGap(123, 123, 123))
                    .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(9, 9, 9)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jScrollPane2, 35,35, Short.MAX_VALUE)
                .addContainerGap())
        );

    }

    
//    private void bt_ajouterActionPerformed(java.awt.event.ActionEvent evt) {
//      //  String [] t ={"nom","objectif","date debut","date fin"};
//      //  md.add(t);
//      //  tbl_principal.repaint();
//     /*   popup_release popup = new popup_release(Release);
//        base_affichage ba = new base_affichage(popup);
//        ba.getMainPanel().setLayout(new VerticalBagLayout());
//        ba.getMainPanel().add(popup);
//        ba.setVisible(true);*/
//    }


    public int[] getDureeTaches(CSprint m_sprint ){
        java.util.Iterator<Histoire> ithist = m_sprint.getlisteHistoire().iterator();
            Histoire hist;
            Tache tache;
            int i=0;
           int nbJours=m_sprint.getDuree();
            int [] tab = new int[nbJours];
            while(ithist.hasNext())
            {
            hist = ithist.next();

             java.util.Iterator<Tache> ittache = hist.getListTaches().iterator();
                while(ittache.hasNext()&& i<nbJours)
                {
                tache=ittache.next();
                tab[i]+=tache.getTpsParJour(i);
                i++;
                }

    }
    return tab;

}
    
    public int[] getTabJours(CSprint m_sprint){
        int  nbJours = m_sprint.getDuree();
        int [] tabJours  = new int [nbJours];
         for(int i=0;i<nbJours;i++)
         {tabJours[i]=i;}
        return tabJours;
     }
    
    
    

      protected void bt_graphActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
    	  int sel = ligneSelectionnee();
    	CSprint s= (CSprint)(controle.getM_listeSprint().get(sel));
    	//getDureeTaches(s),getTabJours(s)
    	System.out.println("pt test 14");
    	 double []ta={0,1,2,3,4,5};
         double []tb={12,10,7,3,1,0};
    	final DualAxisChart chart = new DualAxisChart("title",ta,tb);
        chart.pack();
        RefineryUtilities.centerFrameOnScreen(chart);
        chart.setVisible(true);

    	 	
    	
    	
    	
      
	}

	private void bt_supprActionPerformed(java.awt.event.ActionEvent evt) {
      int sel = ligneSelectionnee();
      int n = JOptionPane.showConfirmDialog(null,"Voulez-vous vraiment supprimer ce sprint","Confirmation",JOptionPane.YES_NO_OPTION);
      if(n==JOptionPane.YES_OPTION)
      {

      controle.getM_listeSprint().remove(sel);
          }
     updateliste();
      tbl_principal.repaint();
    }




   public void updateliste()
    {
    md.removeAll();
   Iterator it = controle.getM_listeSprint().iterator();
    Sprint rl;
   while(it.hasNext())
   {
   rl =(Sprint)it.next();
   Integer i= new Integer(rl.getDuree());
   String [] t ={rl.getNom(),i.toString(),rl.getDateDebut().toString(),rl.getDateFin().toString(),rl.getObjectifs()};
    md.add(t);

   }
    mda.removeAll();
    
 
     String [] v ={controle.getNom(),controle.getObjectif(),controle.getDateDebut().toString(),controle.getDateFin().toString()};
    mda.add(v);
        
//  System.out.println("update ");
    this.repaint();
    tbl_principal.repaint();
    if( controle.getPresentation()!=null)
    controle.getPresentation().repaint();
    }

   

    private void bt_detailActionPerformed(java.awt.event.ActionEvent evt) {
      int sel = ligneSelectionnee();
      DetailSprint frame_detailSprint = new DetailSprint(controle.getM_listeSprint().get(sel),controle.getProjet().getBacklog());
      frame_detailSprint.setVisible(true);
    //CSprint s= (CSprint)(controle.getM_listeSprint().get(sel));
        // popup_release popup = new popup_release(projet);
      /*  base_affichage ba = new base_affichage(s.getPresentation());
        ba.getMainPanel().setLayout(new VerticalBagLayout());
        ba.getMainPanel().add(s.getPresentation());
        ba.setVisible(true);*/
    }

     private void bt_modifierActionPerformed(java.awt.event.ActionEvent evt) {
        int sel = ligneSelectionnee();
        if (sel != -1) {
        	popup_sprint pop = new popup_sprint(controle.getSprint(sel), this);
        	pop.setVisible(true);
        }
    }
      public javax.swing.JTable getTable()
      {
          return  tbl_principal;
      }
    public int ligneSelectionnee(){
        return tbl_principal.getSelectedRow();
    }

    public void update(int sel){
     /*
        ((model_table_backlog)(tbl_principal.getModel())).update(sel, ((CBacklog)controle).getHistoire(sel).getNom(), ((CBacklog)controle).getHistoire(sel).getDescription(), ((CBacklog)controle).getHistoire(sel).getDuree(),((CBacklog)controle).getHistoire(sel)getTaches());
        tbl_principal.repaint();*/
    }

 /*  public void updateAffichage(){
    	md.removeAll();
    	int i = Release.getListeRelease().size();
    	while (i>0){
    		i--;
			Release r = Release.getListeRelease().get(i);
    		String[] t = {r.getNom(), r.getObjectif(), String.valueOf(r.getDateDebut()), String.valueOf(r.getDateFin())};
    		md.add(t);
    		tbl_principal.repaint();
    	}
    	tbl_principal.repaint();
    }*/



    // Variables declaration - do not modify
    private JPanel panelVertical;
    private javax.swing.JButton bt_ajouter;
    private javax.swing.JButton bt_detail;
    private javax.swing.JButton bt_modifier;
    private javax.swing.JButton bt_suppr;
    private javax.swing.JButton bt_graph;
    private javax.swing.JButton bt_monter;
    private javax.swing.JButton bt_descendre;

    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tbl_principal;
   private javax.swing.JTable tbl_active;
    // End of variables declaration


    public boolean valider() {
    	return true;
    }


}

